<template>
	<a-layout>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="content">请假申请</block>
		</cu-custom>
		<br/>
		<br/>
		<br/>
		<y-form radius="radius" :keys="columns" v-model="info" @change="change" @submit="submit">
			<template #startTime="{keys:{key}}">
				<datetime-picker style="flex:1;" defaultValue="" placeholder="请选择" fields="minute" @change="dateChange($event,key)" />
			</template>
			<template #endTime="{keys:{key}}">
				<datetime-picker style="flex:1;" defaultValue="" placeholder="请选择" fields="minute" @change="dateChange($event,key)" />
			</template>
		</y-form>
	</a-layout>
</template>

<script>
	import common from './common.js';
	import {format} from '@/a/utils/api.js';
	export default {
		mixins: [common],
		methods: {
			submit(e) {
				if(!e) return ;
				this.change(this.info);
				const x = {...this.info};
				x.startTime = new Date(x.startTime);
				x.endTime = new Date(x.endTime);
				this.$http.post('/api/v1/stuLeaveRecord/addStuLeaveRecord', { ...x,stuId: this.$store.state.userInfo.id}).then(res => {
					uni.showToast({
						duration:1500,
						title:"提交成功"
					});
					setTimeout(e=>{
						uni.redirectTo({
							url:'/pages/AskForLeave/AskForLeave'
						})
					},1000);
				}).finally(this.$loading({msg:'提价中'}));
			},
			dateChange({
				f3
			}, key) {
				this.$set(this.info, key, format(f3));
				this.change(this.info);
			},
			change(e) {
				//计算请假天数
				if (e.endTime && e.startTime) {
					const time = new Date(e.endTime) - new Date(e.startTime);
					if (time < 0) {
						uni.showToast({
							title: '结束时间不能小于开始时间',
							duration: 2000,
							icon: "none"
						});
					} else {
						//this.$set(this.info, 'daysOff', Math.round(time / (24 * 3600 * 1000), 2));
					}
				}
			}
		}
	};
</script>
